<?php 
class ControllerMessageActivity extends Controller {
	public function index() {  
		$this->language->load('message/activity');

		$this->load->model('message/activity');

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
			'separator' => false
		);

		if (isset($this->request->get['activity_id'])) {
			$activity_id = (int)$this->request->get['activity_id'];
		} else {
			$activity_id = null;
		}
		
		$data = array(
			"activity_id"=>$activity_id,
		);
		

		$article_info = $this->model_message_activity->getActivity($data);

		if ($article_info) {
			$this->document->setTitle($article_info['title']);

			$this->data['breadcrumbs'][] = array(
				'text'      => $article_info['title'],
				'href'      => $this->url->link('message/article', 'activity_id=' .  $activity_id),      		
				'separator' => $this->language->get('text_separator')
			);

			$this->data['heading_title'] = $article_info['title'];
			$this->data['button_continue'] = $this->language->get('button_continue');
			$this->data['summary'] = html_entity_decode($article_info['summary'], ENT_QUOTES, 'UTF-8');
			$this->data['description'] = html_entity_decode($article_info['description'], ENT_QUOTES, 'UTF-8');
			$this->data['enter'] = $this->url->link('message/activity/enter','activity_id='.(int)$article_info['activity_id'],'SSL');
			$this->data['is_over'] = strtotime($article_info['date_end'])>=time()?false:true;
			$this->data['is_can_enter'] = ((int)$article_info['limit']-(int)$article_info['used'])>0?TRUE:false;
			
		} else {
			$this->data['heading_title'] = '';
			$this->data['description'] = '没有相关信息';
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/activity.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/message/activity.tpl';
			} else {
				$this->template = 'default/template/message/activity.tpl';
			}

			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);

			$this->response->setOutput($this->render());
	}
	
	public function enter(){
		$this->language->load('message/activity');
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->load->model('message/activity');
		//如果已经登录
		
		if(isset($this->request->get['activity_id'])){
			$activity_id=(int)$this->request->get['activity_id'];
		}else{
			$activity_id = 0;
		}
		$this->data['account'] = $this->url->link('account/account','','SSL');
		$this->data['activitys'] = $this->url->link('message/activity/activity_list','','SSL');
		$this->data['error'] = $this->language->get('text_check_error');
		$this->data['back'] = $this->url->link('message/activity/activity_list','','SSL');
		if(!$this->customer->isLogged()){
			$this->session->data['redirect'] = $this->url->link('message/activity', 'activity_id='.$activity_id, 'SSL');
			$this->redirect($this->url->link('account/login', '', 'SSL'));
		}else{
			//如果申请过,提示已经加入
			if($this->model_message_activity->check($activity_id,$this->customer->getId())){
				//如果没有申请过，并且名额没有满，成功申请
				$this->model_message_activity->enter($activity_id,$this->customer->getId());
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/activity_success.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/message/activity_success.tpl';
				} else {
					$this->template = 'default/template/message/error.tpl';
				}
				
			}else{
				
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/activity_error.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/message/activity_error.tpl';
				} else {
					$this->template = 'default/template/message/error.tpl';
				}
			}
				$this->children = array(
					'common/column_left',
					'common/column_right',
					'common/content_top',
					'common/content_bottom',
					'common/footer',
					'common/header'
				);
				$this->response->setOutput($this->render());
		}
		
	}

	public function activity_list() {
		$this->language->load('message/activity');
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->load->model('message/activity');
		$this->load->model('tool/image');
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$limit = $this->config->get('config_catalog_limit');
		$data = array(
				'start'             => ($page - 1) * $limit,
				'limit'             => $limit
		);
		
		$total = $this->model_message_activity->getTotalActivitys($data);
		
		$results=$this->model_message_activity->getAllActivitys($data);
		
		foreach ($results as $article) {
			$abPic = "";
			if ($article['pic_url'] && file_exists(DIR_IMAGE . $article['pic_url'])) {
				$abPic = $this->model_tool_image->resize($article['pic_url'], 100, 100);
			} else {
				$abPic = $this->model_tool_image->resize('no_image.jpg', 100, 100);
			}
			$this->data['articles'][] = array(
				'activity_id'=>$article['activity_id'],
				'title'=>$article['title'],
				'description'=>$article['description'],
				'pic_url'=>$article['pic_url'],
				'abPic' =>$abPic,
				'url' =>HTTP_SERVER.$article['url'],
				'url_type' =>$article['url_type'],
				'date_start' =>$article['date_start'],
				'date_end' =>$article['date_end'],
			);
		}
		
		$pages = 0;
		$inPages = $total/$limit ;
		$moPages = $total%$limit;
		if($moPages){
			$pages = $inPages+1;
		}else{
			$pages = $inPages;
		}
		
		$this->data['pages'] = $moPages;
		
		
		$more = ($page+1);
		$before = ($page-1);
		$this->data['has_before'] =  true;
		if($before<1){
			$this->data['has_before'] = false;
			$before = 1;
		}
		$this->data['has_more'] = true;
		if($more>$pages){
			$more = $pages;
			$this->data['has_more'] = false;
		}

		
		$this->data['more'] = $this->url->link('message/activity/activity_list','page='.$more,'SSL');
		$this->data['before'] = $this->url->link('message/activity/activity_list','page='.$before,'SSL');
		
		$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
		);
		

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/activity_list.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/message/activity_list.tpl';
		} else {
			$this->template = 'default/template/message/activity_list.tpl';
		}
		
		$this->response->setOutput($this->render());
	}
}
?>